// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import AudioManager from "./Audio";
import FadePanel from "./FadePanel";
import GameData from "./GameData";
import SceneLoad from "./SceneLoad";
import BannerAd from "./Zsrc/BannerAd";
import TreasureBox from "./Zsrc/TreasureBox";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MainScene extends cc.Component {

    @property(cc.Node)
    mainBg: cc.Node = null;

    @property(cc.Node)
    main2Bg: cc.Node = null;

    @property(cc.Node)
    playBtn: cc.Node = null;

    @property([cc.Label])
    dataLabels: cc.Label[] = [];

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        if (GameData.isBack) this.main2Bg.active = true;
        GameData.isBack = true;
        for (let i = 0; i < this.dataLabels.length; i++) {
            const element = this.dataLabels[i];
            element.string += GameData.Instance.Datas[i];
            if (i == 2) element.string += "/36";
        }
        if (BannerAd.Instance.cityIswhite) BannerAd.Instance.ShowBanner();
    }

    start() {
        if (GameData.isBack) FadePanel.Instance.node.children[0].active = true;
        FadePanel.Instance.FadeOut();
        FadePanel.Instance.node.parent.children[2].getComponent(cc.AudioSource).play();
    }

    // update (dt) {}

    Play() {
        this.playBtn.active = false;
        this.mainBg.getComponent(cc.Widget).enabled = false;
        FadePanel.Instance.FadeIn(FadePanel.Instance.FadeOut);
        cc.tween(this.mainBg)
            .to(1, { scale: 1.5 })
            .call(() => {
                this.main2Bg.active = true;
            })
            .start();
        AudioManager.Instance.ButtonAudio();
        if (BannerAd.Instance.cityIswhite) BannerAd.Instance.ShowBanner();
    }

    Back() {
        this.playBtn.active = true;
        this.mainBg.getComponent(cc.Widget).enabled = true;
        this.mainBg.scale = 1;
        FadePanel.Instance.FadeIn(() => {
            this.main2Bg.active = false;
            FadePanel.Instance.FadeOut();
        });
        AudioManager.Instance.ButtonAudio();
    }

    Game() {
        function a() {
            FadePanel.Instance.FadeIn(() => {
                SceneLoad.GoScene("GameScene");
            })
        }
        if (BannerAd.Instance.cityIswhite) TreasureBox.Show(a);
        else a();
        AudioManager.Instance.ButtonAudio();
    }

    Upgrade() {
        FadePanel.Instance.FadeIn(() => {
            SceneLoad.GoScene("UpgradeScene");
        })
        AudioManager.Instance.ButtonAudio();
    }

    Hide() {

    }

    Kefu() {

    }

    More() {

    }
}
